/** @file    EncodersXmlQt_Communication.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncodersXmlQt_Communication.cpp 2743 2010-07-07 20:52:20Z alex $
 *  @brief
 * The header for this class can be found in EncodersXmlQt_Communication.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/qt/EncodersXmlQt_Communication.h>

 #include <framework/persistence/qt/EncodersXmlQt_Core.h>
 #include <core/ObjectAllocator.h>


namespace wosh {
 namespace persistence {
  namespace qt {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_AudioMessage, "wosh::communication::AudioMessage", 0.0, _static_EncoderXml_Qt_Communication_AudioMessage )

WRESULT EncoderXml_Qt_Communication_AudioMessage::serializeTo( const wosh::communication::AudioMessage* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Communication_FileMessage::serializeTo(object, doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "audio-type", (int)object->getAudioType(), doc, contNode, encoder_options );
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_AudioMessage::deserializeFrom( wosh::communication::AudioMessage* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Communication_FileMessage::deserializeFrom(object, contNode );
	object->setAudioType( (wosh::communication::AudioMessage::AUDIO_TYPE)EncoderBaseXmlQt::deserializeLong( "audio-type", (int)wosh::communication::AudioMessage::AUDIO_GENERIC, contNode ) );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_ContactInfo, "wosh::communication::ContactInfo", 0.0, _static_EncoderXml_Qt_Communication_ContactInfo )

WRESULT EncoderXml_Qt_Communication_ContactInfo::serializeTo( const wosh::communication::ContactInfo* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	EncoderBaseXmlQt::serializeString( "userID", object->getUserID(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "userWosh", object->getUser(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "last-seen", object->getLastSeen(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "user-presence", (int)object->getUserPresence(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "user-status", (int)object->getUserStatus(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "status-message", object->getStatusMessage(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "protocol", (int)object->getProtocol(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "language", object->getLanguage(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "lastTargetWoshUser", object->getLastTargetUser(), doc, contNode, encoder_options );
	return WRET_OK;
 }

WRESULT EncoderXml_Qt_Communication_ContactInfo::deserializeFrom( wosh::communication::ContactInfo* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	object->setUserID( EncoderBaseXmlQt::deserializeString( "userID", "", contNode ) );
	object->setUser( EncoderBaseXmlQt::deserializeString( "userWosh", "", contNode ) );
	object->setLastSeen( EncoderBaseXmlQt::deserializeLong( "last-seen", 0, contNode ) );
	object->setUserPresence( (wosh::User::PRESENCE)EncoderBaseXmlQt::deserializeLong( "user-presence", 0, contNode ) );
	object->setUserStatus( (wosh::User::STATUS)EncoderBaseXmlQt::deserializeLong( "user-status", 0, contNode ) );
	object->setStatusMessage( EncoderBaseXmlQt::deserializeString( "status-message", "", contNode ) );
	object->setProtocol( (wosh::Notification::PROTOCOL)EncoderBaseXmlQt::deserializeLong( "protocol", (int)wosh::Notification::PROTOCOL_UNKNOWN, contNode ) );
	object->setLanguage( EncoderBaseXmlQt::deserializeString( "language", "", contNode ) );
	object->setLastTargetUser( EncoderBaseXmlQt::deserializeString( "lastTargetWoshUser", "", contNode ) );
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_FileMessage, "wosh::communication::FileMessage", 0.0, _static_EncoderXml_Qt_Communication_FileMessage )

WRESULT EncoderXml_Qt_Communication_FileMessage::serializeTo( const wosh::communication::FileMessage* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::serializeTo(object, doc, contNode, encoder_options );
	if ( isCompact(encoder_options) )
		contNode.setAttribute("resource", object->getFile().getPathFull().c_str() );
	else {
		QDomElement resource_node = doc.createElement("resource"); contNode.appendChild(resource_node);
		ret = EncoderXml_Qt_ResourcePath::serializeTo(&object->getFile(), doc, resource_node, true );
	 }
	EncoderBaseXmlQt::serializeString( "plain", object->getPlain(), doc, contNode, encoder_options );
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_FileMessage::deserializeFrom( wosh::communication::FileMessage* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::deserializeFrom(object, contNode );
	QDomElement resource_node = contNode.namedItem("resource").toElement();
	if ( !resource_node.isNull() )
		ret += EncoderXml_Qt_ResourcePath::deserializeFrom(&object->getFile(), resource_node );
	else if ( contNode.hasAttribute("resource") )
		object->getFile().set( contNode.attribute("resource").toLatin1().data() );
	object->setPlain( EncoderBaseXmlQt::deserializeString( "plain", "", contNode ) );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_NotificationConversion, "wosh::communication::NotificationConversion", 0.0, _static_EncoderXml_Qt_Communication_NotificationConversion )

WRESULT EncoderXml_Qt_Communication_NotificationConversion::serializeTo( const wosh::communication::NotificationConversion* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;
	if ( object->hasOriginal() ) {
		QDomElement original_node = doc.createElement("original-nofitication"); contNode.appendChild(original_node);
		ret += EncoderProtocolXmlQt::serializeTo( object->getOriginal(), doc, original_node, encoder_options);
	 }
	EncoderBaseXmlQt::serializeString( "language", object->getLanguage(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "protocol", (int)object->getProtocol(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "type", (int)object->getNotificationType(), doc, contNode, encoder_options );
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_NotificationConversion::deserializeFrom( wosh::communication::NotificationConversion* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;
	QDomElement original_node = contNode.namedItem("original-nofitication").toElement();
	if ( !original_node.isNull() ) {
		Notification* notif = EncoderProtocolXmlQt::deserializeFrom<Notification>( original_node, &ret );
		object->setOriginal(notif);
	 }
	object->setLanguage( EncoderBaseXmlQt::deserializeString( "language", "", contNode ) );
	object->setProtocol( (wosh::Notification::PROTOCOL)EncoderBaseXmlQt::deserializeLong( "protocol", (int)wosh::Notification::PROTOCOL_UNKNOWN, contNode ) );
	object->setNotificationType( (wosh::Notification::TYPE)EncoderBaseXmlQt::deserializeLong( "type", (int)wosh::Notification::TYPE_UNKNOWN, contNode ) );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_NotificationFlow, "wosh::communication::NotificationFlow", 0.0, _static_EncoderXml_Qt_Communication_NotificationFlow )

WRESULT EncoderXml_Qt_Communication_NotificationFlow::serializeTo( const wosh::communication::NotificationFlow* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;
	EncoderBaseXmlQt::serializeLong( "id", object->getID(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeBool( "archived", object->isArchived(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "processing-ts", object->getProcessingTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "processing-count", object->getProcessingCount(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "errors-ts", object->getErrorTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "errors-count", object->getErrorsCount(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "sent-ts", object->getSentTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "sent-count", object->getSentCount(), doc, contNode, encoder_options );
	if ( !isCompact(encoder_options) || !object->getSourceCommunicator().isEmpty() ) {
		if ( isCompact(encoder_options) )
			contNode.setAttribute("communicator-source", object->getSourceCommunicator().toString().c_str() );
		else {
			QDomElement source_node = doc.createElement("communicator-source"); contNode.appendChild(source_node);
			ret = EncoderXml_Qt_URI::serializeTo(&object->getSourceCommunicator(), doc, source_node, encoder_options );
		 }
	 }
	if ( !isCompact(encoder_options) || !object->getSenderCommunicator().isEmpty() ) {
		if ( isCompact(encoder_options) )
			contNode.setAttribute("communicator-sender", object->getSenderCommunicator().toString().c_str() );
		else {
			QDomElement sender_node = doc.createElement("communicator-sender"); contNode.appendChild(sender_node);
			ret = EncoderXml_Qt_URI::serializeTo(&object->getSenderCommunicator(), doc, sender_node, encoder_options );
		 }
	 }
	if ( !isCompact(encoder_options) || object->hasOriginal() ) {
		QDomElement original_node = doc.createElement("original-nofitication"); contNode.appendChild(original_node);
		if ( object->hasOriginal() )
			ret += EncoderProtocolXmlQt::serializeTo( object->getOriginal(), doc, original_node, encoder_options);
	 }
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_NotificationFlow::deserializeFrom( wosh::communication::NotificationFlow* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;
	object->setID( EncoderBaseXmlQt::deserializeLong( "id", 0, contNode ) );
	object->setArchived( EncoderBaseXmlQt::deserializeBool( "archived", false, contNode ) );
/*
	EncoderBaseXmlQt::serializeLong( "processing-ts", object->getProcessingTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "processing-count", object->getProcessingCount(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "errors-ts", object->getErrorTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "errors-count", object->getErrorsCount(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "sent-ts", object->getSentTs(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "sent-count", object->getSentCount(), doc, contNode, encoder_options );
*/
	QDomElement source_node = contNode.namedItem("communicator-source").toElement();
	if ( !source_node.isNull() )
		ret += EncoderXml_Qt_URI::deserializeFrom(&object->getSourceCommunicator(), source_node );
	else if ( contNode.hasAttribute("communicator-source") )
		object->getSourceCommunicator().fromString( contNode.attribute("communicator-source").toLatin1().data() );
	QDomElement sender_node = contNode.namedItem("communicator-sender").toElement();
	if ( !sender_node.isNull() )
		ret += EncoderXml_Qt_URI::deserializeFrom(&object->getSenderCommunicator(), sender_node );
	else if ( contNode.hasAttribute("communicator-sender") )
		object->getSenderCommunicator().fromString( contNode.attribute("communicator-sender").toLatin1().data() );

	QDomElement original_node = contNode.namedItem("original-nofitication").toElement();
	if ( !original_node.isNull() ) {
		Notification* notif = EncoderProtocolXmlQt::deserializeFrom<Notification>( original_node, &ret );
		object->setOriginal(notif);
	 }
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_TextMessage, "wosh::communication::TextMessage", 0.0, _static_EncoderXml_Qt_Communication_TextMessage )

WRESULT EncoderXml_Qt_Communication_TextMessage::serializeTo( const wosh::communication::TextMessage* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::serializeTo(object, doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "subject", object->getSubject(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeString( "plain", object->getPlain(), doc, contNode, encoder_options );
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_TextMessage::deserializeFrom( wosh::communication::TextMessage* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::deserializeFrom(object, contNode );
	object->setSubject( EncoderBaseXmlQt::deserializeString( "subject", "", contNode ) );
	object->setPlain( EncoderBaseXmlQt::deserializeString( "plain", "", contNode ) );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Communication_VoiceCall, "wosh::communication::VoiceCall", 0.0, _static_EncoderXml_Qt_Communication_VoiceCall )

WRESULT EncoderXml_Qt_Communication_VoiceCall::serializeTo( const wosh::communication::VoiceCall* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::serializeTo(object, doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "duration", object->getDuration(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "call-status", (int)object->getCallStatus(), doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "call-type", (int)object->getCallType(), doc, contNode, encoder_options );
	return ret;
 }

WRESULT EncoderXml_Qt_Communication_VoiceCall::deserializeFrom( wosh::communication::VoiceCall* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Notification::deserializeFrom(object, contNode );
	object->setDuration( EncoderBaseXmlQt::deserializeLong( "duration", 0, contNode ) );
	object->setCallStatus( (wosh::communication::VoiceCall::CALL_STATUS)EncoderBaseXmlQt::deserializeLong( "call-status", (int)wosh::communication::VoiceCall::STATUS_UNKNOWN, contNode ) );
	object->setCallType( (wosh::communication::VoiceCall::CALL_TYPE)EncoderBaseXmlQt::deserializeLong( "call-type", (int)wosh::communication::VoiceCall::CALL_UNKNOWN_TYPE, contNode ) );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace qt
 }; // namespace persistence
}; // namespace wosh
